//
//  HZContionOrderView.m
//  Stock_Stategy
//
//  Created by MayerF on 2018/1/24.
//  Copyright © 2018年 MayerF. All rights reserved.
//

#import "HZContionOrderView.h"

#import "MYRadioGroup.h"
#import "HZOrderTableViewCell.h"
#import "MYUpDownBtn.h"

@interface HZContionOrderView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)MYRadioGroup    *pRadioGroup;
@property (nonatomic, strong)UITableView      *pTableView;
@property (nonatomic, strong)NSArray          *pDataList;

@property(nonatomic,strong)MYUpDownBtn    *pAlertInfo;
@end

@implementation HZContionOrderView
#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
        self.backgroundColor = kMYHexToRGB(0xf5f5f5, 1);
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)commonInit{
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    _pRadioGroup = [MYRadioGroup new];
    
    _pRadioGroup.parent = self;
    _pRadioGroup.backgroundColor = kMYHexToRGB(0xefefef, 1);
    [_pRadioGroup setRadioGroup:@[@"生效中",@"已触发",@"已失效"]];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:YES];
    [_pRadioGroup setSelectedButton:0];
    
    [self addSubview:_pRadioGroup];
    [self addSubview:_pTableView];
    
    [self p_initConstraints];
    
}

- (void)p_initConstraints
{
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(45);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.top.mas_equalTo(_pRadioGroup.mas_bottom);
    }];
}

- (void)updateNewData:(NSArray *)data
{
    if (data && data.count) {
        _pAlertInfo.hidden = YES;
    }else {
        [self p_loadAlertInfo];
    }
    _pDataList = data;
    [_pTableView reloadData];
    if (_pDataList.count > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_pTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
- (void)p_loadAlertInfo{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateHighlighted];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
        [_pAlertInfo setTitle:@"暂无数据" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(70);
        }];
    }
    _pAlertInfo.hidden = NO;
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    HZOrderQueryModel *model = _pDataList[indexPath.row];
    HZOrderTableViewCell *cell = [HZOrderTableViewCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 280;
}

- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter {
    switch (type) {
        case kHZ_radioGroupClick_Action:
        {
            UIButton *btn = (UIButton *)paramter;
            LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterFinished_clickHeader];
            if (block) {
                block(nil, btn.currentTitle);
            }
        }
            break;
            
        default:
            break;
    }
}

@end
